<?php
include "otamu_config.php";
$startTime = slog_time();

$db =& ADONewConnection('access');
	$dsn = "Driver={Microsoft Access Driver (*.mdb)};Dbq=d:\\SMPK.mdb;Uid=Administrator;Pwd=;";
	$db->Connect($dsn);
$db->SetFetchMode(ADODB_FETCH_ASSOC);

class Institusi extends ADOdb_Active_Record
{
	var $_table = "master_institusi";
}


//MSACCESS => PSQL table
$institusi_map = array (
	"KOD_INSTITUSI" => "kod",
	"NAMA_INSTITUSI" => "nama",
	"ALAMAT" => "alamat",
	"POSKOD" => "poskod",
	"KOD_KATEGORI_INSTITUSI" => "agensi_pelaksana_id",
	"KOD_NEGERI" => "negeri_id",
	"KOD_DAERAH" => "kod_daerah",
	"EMEL" => "email",
	"LAMAN_WEB" => "url",
	"KODLOKASI" => "lokasi_id",
	"TARIKH_TUBUH" => "tarikh_tubuh",
	"KOD_JENIS_INSTITUSI" => "kod_jenis_institusi",
	"KOD_BAHASA_PENGHANTAR" => "kod_bahasa_penghantar",
	"KOD_KAWASAN_PARLIMEN" => "parlimen_id",
	"KOD_DUN" => "dun_id",
	"KOD_PPD" => "kod_ppd",
	"PRESCHOOL" => "prasekolah",
	"AKTIF" => "aktif",
	"MAKLUMAT_OKU" => "maklumat_oku_id",
	"KOD_GRED" => "gred_institusi_id"
	
);

//set wanted fields
$selected_field = "";
foreach ($institusi_map as $k=>$v) {
	$arr[] = $k;
}
$selected_field = join(",",$arr);


$rs = $db->Execute("SELECT $selected_field FROM SPK_SEKOLAH WHERE KOD_NEGERI in ('01')");
//$rs = $db->Execute("SELECT $selected_field FROM SPK_SEKOLAH");
$i=0;	
while (!$rs->EOF) {
	$i++;

	$institusi = new Institusi();
	foreach ($institusi_map as $k => $v) {
		echo "$i : $k => " .$rs->fields[$k] . "<br>\n";
		$institusi->$v = $rs->fields[$k];

		if ($rs->fields[$k] == 'NULL') {
			$institusi->$v = '0';
		}
	}
	

	if (!$institusi->save()) { 
		echo $institusi->ErrorMsg();exit;
		$_SESSION['flash'] = $institusi->ErrorMsg();
	}else {
		$_SESSION['flash'] = 'Institusi telah diwujudkan.';
	}

    $rs->MoveNext();
} 

    $totalTime = elog_time($startTime);
     echo  "Execution Time: $totalTime Seconds";

exit;


?>